说在前面
因为NCNN的部署是不依赖任何第三方库的,所以在模型部署的过程中,只需要了解CMake编译工程的步骤。具体详见CMake 入门实践
下载ResNet-50模型
caffe model zoo中有对应的各种不同版本的训练好的model,因为是部署,所以我们需要的仅仅是对应的caffemodel文件和deploy.prototxt文件。
转换ncnn网络和模型
- 首先建立resnet源文件目录
1 |
|
将下载好的ResNet-50源文件复制到model文件夹中如下所示:1
2$ root@a488d431ffee:/home/dl/ncnn/examples/resnet/model# ls
ResNet-50-deploy.prototxt ResNet-50-model.caffemodel
- caffe 自带了工具可以把老版本的 caffe 网络和模型转换为新版(ncnn的工具只认识新版)
1 | upgrade_net_proto_text ResNet-50-deploy.prototxt ncnn-ResNet-50-deploy.prototxt |
- 输入层改用 Input,因为每次只需要做一个图片,所以第一个 dim 设为 1
1 | layer { |
- 使用 caffe2ncnn 工具转换为 ncnn 的网络描述和模型
注意caffe2ncnn工具在
1 | $ cd <ncnn-root-dir> |
准备工作全部完成如下,我们最终需要的是resnet-50.bin
和resnet-50.param
两个文件:1
2root@a488d431ffee:/home/dl/ncnn/examples/resnet/model# ls
ResNet-50-deploy.prototxt ResNet-50-model.caffemodel ncnn-ResNet-50-deploy.prototxt ncnn-ResNet-50-model.caffemodel resnet-50.bin resnet-50.param
建立ResNet工程
需要修改对应的CMakeLists.txt
首先修改examples文件夹下的CMakeLists.txt文件
1
2# 最后一行添加
add_subdirectory(resnet)然后进入resnet文件夹,创建CMakeLists.txt文件
1
2
3
4
5
6
7
8
9
10
11find_package(OpenCV REQUIRED core highgui )
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../src)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../src)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(resnet-50 resnet_50.cpp)
target_link_libraries(resnet-50 ncnn ${OpenCV_LIBS})
编写resnet_50.cpp(仿照SqueezeNet)
1 |
|
运行程序
1 | $ cd <ncnn-root-dir> |
结果如下:
1 | Pembroke, Pembroke Welsh corgi |
参考
CMake 入门实践
caffe model zoo
ncnn 组件使用指北 alexnet
Ubuntu16.04—腾讯NCNN框架入门到应用